﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace EzDesk.DevHelper.DbMgr
{
    public static class Utility
    {
        public static string ExecuteCmd(string cmdText)
        {
            ProcessStartInfo ps = new ProcessStartInfo("cmd", "/c " + cmdText);
            ps.UseShellExecute = false;
            ps.CreateNoWindow = true; //加这一句   
            ps.RedirectStandardOutput = true;
            Process p = Process.Start(ps);
            p.WaitForExit();
            return p.StandardOutput.ReadToEnd();
        }

        /// <summary>
        /// 寻找EzDesk项目跟文件夹,通过EzDesk-dev.sln文件定位
        /// </summary>
        /// <returns>找不到返回empty</returns>
        public static string GetEzDeskRootPath()
        {
            string fileName = "EzDesk-Dev.sln";
            string pathStr = Directory.GetCurrentDirectory();
            do
            {
                string fullFileName = string.Format(
                    "{0}\\{1}",
                    pathStr,
                    fileName
                    );
                if (File.Exists(fullFileName))
                {
                    // 找到文件
                    break;
                }

                DirectoryInfo parentInfo = Directory.GetParent(pathStr);
                if (parentInfo != null)
                {
                    pathStr = parentInfo.FullName;
                }
                else
                {
                    pathStr = string.Empty;
                    break;
                }
            } while (true);

            return pathStr;
        }
    }
}
